#include "test_misc.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"



void cm_test_misc_operation(unsigned char*cmd[],int len)
{
    if(0 == strcmp("STR_BCD",cmd[2]))
    {
        char *pcchStr = "0123456789987654321045678932155ABCDEF";
        uint32_t u32StrLen = 37;
	    uint8_t pu8BcdBuf[20] = {0};
        uint32_t u32BufLen = 20;
        uint32_t ret = cm_misc_bcd_from_str(pcchStr,u32StrLen,pu8BcdBuf,u32BufLen);
        cm_demo_printf("cm_misc_bcd_from_str ret:%d\n",ret);
        for(int i = 0;i<ret;++i)
        {
            cm_demo_printf("%#x, ",pu8BcdBuf[i]);
        }
        cm_demo_printf("\n");
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,ret,cm_misc_bcd_from_str);

        const uint8_t* pcu8Bcd = pu8BcdBuf;
        uint32_t u32BcdLen = ret; 
        char pchStrBuf[40] = {0};
        u32BufLen = 40;
        uint32_t ret_2 = cm_misc_bcd_to_str(pcu8Bcd,u32BcdLen,pchStrBuf,u32BufLen);
        cm_demo_printf("cm_misc_bcd_to_str ret:%d\n",ret_2);
        for(int j = 0;j < ret_2;++j)
        {
            cm_demo_printf("%c, ",pchStrBuf[j]);
        }
        cm_demo_printf("\n");
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(0,ret,cm_misc_bcd_to_str);
    }
    else if(0 == strcmp("HEX_BCD",cmd[2]))
    {
        uint8_t HEX[32] = {1,2,3,4,5,6,7,8,9,0,0xa,0xb,0xc,0xd,0xe,0xf,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
        uint8_t BCD[32] = {0};
        for (int i = 0;i<32;++i)
        {
            BCD[i] = cm_misc_hex_to_bcd(HEX[i]);
            cm_demo_printf("%#x, ",BCD[i]);
        }
        cm_demo_printf("\n");
        for(int j = 0;j < 32;++j)
        {
            HEX[j] = cm_misc_bcd_to_hex(BCD[j]);
            cm_demo_printf("%#x, ",HEX[j]);
        }	
        cm_demo_printf("\n");
    }

    else if(0 == strcmp("CRC_8_16_32",cmd[2]))
    {
        const uint8_t pcu8Data[1024] = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM\
1234567890qwertyuiopasdfghjklzxc";
        uint32_t u32Len = 1024;
        uint8_t crc8 = cm_misc_crc8(pcu8Data,u32Len);
        cm_demo_printf("crc8:%hd\n",crc8);
        OCTEST_EXPECT_EQUAL_INT32(0xe0,crc8);
        uint16_t crc16 = cm_misc_crc16(pcu8Data,u32Len);
        cm_demo_printf("crc16:%hu\n",crc16);
        OCTEST_EXPECT_EQUAL_INT32(0xE1CD,crc16);
        // uint32_t crc32 = cm_misc_crc32(pcu8Data,u32Len);
        // cm_demo_printf("crc32:%hd\n",crc32);
        // OCTEST_EXPECT_EQUAL_INT32(0xBB46A4A0,crc32);
    }
    else if(0 == strcmp("SEC_DATE_WEEK",cmd[2]))
    {
        long lSec = 1669862270;
        cm_tm_t tTime = {0};
        cm_sec_to_date(lSec, &tTime);
        cm_demo_printf("cm_sec_to_date no ret\n%d-%d-%d:%d:%d:%d",tTime.tm_year,tTime.tm_mon,tTime.tm_mday,tTime.tm_hour,tTime.tm_min,tTime.tm_sec);
        long lSec_ret = cm_date_to_sec(&tTime);
        cm_demo_printf("lSec:%ld\n",lSec_ret);
        OCTEST_EXPECT_EQUAL_INT32(lSec,lSec_ret);
        uint8_t week = cm_time_to_weekday(&tTime);
        cm_demo_printf("week:%d\n",week);
    }
    else if(0 == strcmp("EIME_WEEK",cmd[2]))
    {
        
    }
    else{
        cm_demo_printf("param error!\n");
    }
}